<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Config;

use think\Request;

/**
 * 数字大会接口
 */
class Xuexin extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

        $this->news=model('admin/xuexin/News');
        $this->activityday=model('admin/xuexin/Activityday');
        $this->activitynews=model('admin/xuexin/Activitynews');
        $this->bolanhui=model('admin/xuexin/Bolanhui');
        $this->weivideo=model('admin/xuexin/Weivideo');
        $this->lijiehuigu=model('admin/xuexin/Lijiehuigu');
        $this->forum=model('admin/xuexin/Forum');
        $this->forumjiabin=model('admin/xuexin/Forumjiabin');

       // xuexin/bolanhui



    }

    /**
     * 基础配置
     */
    public function index(){
        $data['logo']=Config('site.xuexin');

       $this->success('OK',$data);
    }

    /**
     * 首页其他信息
     */
    public function othermsg(){
       $recommendnew=$this->news->where('status','normal')->where('is_home','推荐')->field('id,title,image,time,desc')->limit(0,5)->select();
       foreach ($recommendnew as $key=>$value){
           $recommendnew[$key]['day']=date('d',$value['time']);
           $recommendnew[$key]['year_month']=date('Y-m',$value['time']);

       }
        $data['recommendnew']=$recommendnew;
        $data['hotnews']=$this->news->where('status','normal')->where('is_home','推荐')->where('type','热点动态')->field('id,title,image,time,desc')->order('weigh desc')->limit(0,5)->select();
//
        $data['dynamic']=$this->news->where('status','normal')->where('is_home','推荐')->where('type','各地动态')->field('id,title,image,time,desc')->order('weigh desc')->limit(0,5)->select();
        $activityday=  $this->activityday->where('status','normal')->where('is_home','推荐')->order('weigh desc')->select();
        foreach ($activityday as $key=>$value){
            $activityday[$key]['activitynews']=$this->activitynews->where('xuexin_activityday_id',$value['id'])->where('status','normal')->order('weigh desc')->select();

        }
        $data['activityday']=$activityday;

        $data['formList']=$this->forum->where('status','normal')->where('is_home','推荐')->limit(0,5)->select();
        $data['zhanbohui']=Config('site.zhanbohui');

        $data['zhanbohuiList']= $this->bolanhui->where('status','normal')->where('is_home','推荐')->order('weigh desc')->select();

        $data['weivide_image']=Config('site.weivide_image');

       $data['weivideo']=$this->weivideo->where('status','normal')->where('is_home','推荐')->order('weigh desc')->select();

        $this->success('OK',$data);
    }

    /**
     * 新闻动态
     * @param int $page
     * @param int $limit
     * @param string $type
     */
        public function newList($page=1,$limit=10,$type='全部'){
        $where=[];
        $where['status']=['=','normal'];

        if($type!='全部'){
            $where['type']=['=',$type];
        }
            $newList=$this->news->field('id,title,image,time,desc')->order('weigh desc')->paginate($limit);
            foreach ($newList as $key=>$value){
                $newList[$key]['time_text']=date('Y-m-d',$value['time']);
            }
        $this->success('OK',$newList);
        }

        /***
         * 新闻详情
         */
    public function newDetails($id){

        if($newDetails=$this->news->get($id)){
            $this->success('OK',$newDetails);
        }else{
            $this->error('OK');
        }

    }
    /**
     *活动日程
     */

    public function activitydayList(){
        $where['status']=['=','normal'];
        $data=  $this->activityday->where($where)->order('weigh desc')->select();

        $this->success('OK',$data);
    }
    /**
     * 活动日程新闻列表
     */
    public  function  activitynews($xuexin_activityday_id='全部',$page=1,$limit=10){

        $where=[];
        if($xuexin_activityday_id!='全部'){
            $where['xuexin_activityday_id']=['=',$xuexin_activityday_id];
        }
        $where['status']=['=','normal'];


      $data=  $this->activitynews->where($where)->order('weigh desc')->paginate($limit);

        $this->success('OK',$data);

    }

    /**
     * 活动日程新闻详情
     * @param int $id
     */
  public function activitynewsDetails($id=0){
     if($data=  $this->activitynews->get($id)) {
         $this->success('OK',$data);
     }else{
         $this->error('未找到数据');
     }
  }

    /**
     * 安全博览会
     * @param int $page
     * @param int $limit
     */
      public  function bolanhui($page=1,$limit=10){
          $where['status']=['=','normal'];
         $bolanhuiList= $this->bolanhui->where($where)->order('weigh desc')->paginate($limit);

          $this->success('OK',$bolanhuiList);
      }

    /**
     * 微视频展播
     * @param int $page
     * @param $limit
     */
    public function weivideo($page=1,$limit,$keywords=null){
        $where=[];
        if($keywords!=null){
            $where['title|author|order_sn|company']=['like',"%".$keywords."%"];
        }
        $where['status']=['=','normal'];
        $bolanhuiList= $this->weivideo->where($where)->order('weigh desc')->paginate($limit);

        $this->success('OK',$bolanhuiList);
    }

    /**
     * 等奖名单
     */
    public function goodvideo(){
        $where['status']=['=','normal'];
        $data['first']=$this->weivideo->where($where)->where('type','一等奖')->order('weigh desc')->select();
        $data['second']=$this->weivideo->where($where)->where('type','二等奖')->order('weigh desc')->select();
        $data['third']=$this->weivideo->where($where)->where('type','三等奖')->order('weigh desc')->select();
        $data['youxiu']=$this->weivideo->where($where)->where('is_goods','是')->order('weigh desc')->select();
      $this->success('OK',$data);
    }

    /**微视频展播详情
     * @param $id
     */
    public function weivideoDetails($id){
        $where['id']=['=',$id];
        $bolanhuiList= $this->weivideo->where($where)->order('weigh desc')->find();
        $this->success('OK',$bolanhuiList);

    }

    /**
     *历届回顾
     */
    public function lijiehuigu(){
        $this->success('OK',$this->lijiehuigu->where('status','normal')->order('weigh asc')->select());
    }
/**
 * 论坛详情
 */
 public function forumDetails($id=0,$page=1,$limit=10){
     $data['forumDetails']=$this->forum->get($id);
      $data['forumjiabin']=$this->forumjiabin->where('xuexin_forum_id',$id)->where('status','normal')->order('weigh asc')->select();
     $where['status']=['=','normal'];
     $where['id']=['<>',$id];
     $data['forumList']=$this->forum->where($where)->order('weigh desc')->paginate($limit);;
     $this->success('OK',$data);
 }
}
